//
//  ST_TIANRequirementConfigComponent.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import RxCocoa
import NSObject_Rx
import RxSwift
import SwiftyJSON
import SwiftyUserDefaults

#if !targetEnvironment(simulator)
import IDLFaceSDK
#endif


enum ST_BootConfiguringStatus {
    /// 请求中
    case inLoop
    /// 已经获取到基础配置
    case finished
}

class ST_TIANRequirementConfigComponent:NSObject{
    
    /// 重试延时 1 秒
    private let retry_delay: RxTimeInterval = RxTimeInterval.seconds(1)
    
    
    /// 基础配置状态
    let boot_config_observer = BehaviorRelay<ST_BootConfiguringStatus>(value: ST_BootConfiguringStatus.inLoop)
}

extension ST_TIANRequirementConfigComponent{
    
    func setup_boot_config(){
        
        TIANSession(requestUrl: PH.bootBaseConfigApi).wrap_response_data().retry { error -> Observable<Int> in
            return Observable.timer(self.retry_delay, scheduler: MainScheduler.instance)
        }.subscribe(onNext: { json in
            
            let dataJson = json["data"]
            
            print("Boot 配置无需登录：",dataJson)
            
            let timeArray = dataJson["ios_try_time"].arrayValue.map{ $0.intValue }
            if timeArray.count > 0{
                ST_SwiftConst.shared.provide_content_try_times = timeArray
            }
            
            ST_SwiftConst.shared.hidden_home_trophy = dataJson["hidden_trophy"].boolValue
            
            ST_SwiftConst.shared.dateLogTryTime = dataJson["call_try_time"].intValue
            
            ST_SwiftConst.shared.secretaryJson = dataJson["relation"]
            
            Defaults[.isPanda] = dataJson["shield_panda"].boolValue
             
//            ST_BTLaunchController.shared.setup_tabs()
            
            Defaults[.PandaStr] = dataJson["shield_panda_string"].stringValue
            
            Defaults[.PandaTiStr] = dataJson["shield_panda_mon_string"].stringValue
            
            Defaults[.PandaLinkStr] = dataJson["shield_panda_kind_string"].stringValue
            
            Defaults[.PandaWriteKindStr] = dataJson["tain_write_kind_string"].stringValue
            
            Defaults[.pandaBiuBiuStr] = dataJson["tain_p_biu_biu_string"].stringValue
            
            Defaults[.QNResourcePrefix] = dataJson["qiniu_prefix"].stringValue
            
            SC.toast_fate_chat_config = dataJson["time_set"]
            
            SC.festival_female_icon = dataJson["festival_head_svga_female"].string
            
            SC.festival_male_icon = dataJson["festival_head_svga_male"].string
            
            Defaults.synchronize()
            
            self.boot_config_observer.accept(.finished)
            
            self.setup_baidu_s_d_k()
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    fileprivate func setup_baidu_s_d_k(){
        
        //        百度AI
        let licensePath = Bundle.main.path(forResource: SC.baidu.FACE_LICENSE_NAME, ofType: SC.baidu.FACE_LICENSE_SUFFIX)
        #if !targetEnvironment(simulator)
        FaceSDKManager.sharedInstance()?.setLicenseID(SC.baidu.FACE_LICENSE_ID, andLocalLicenceFile: licensePath, andRemoteAuthorize: true)
        #endif
    }
    
}
